<template>
  <el-dialog v-model="dialogFormVisible" title="增加订阅">
    <el-form :model="form" ref="ruleFormRef" :rules="rules" label-width="120px">
      <el-form-item label="订阅URL" prop="rssLink">
        <el-input v-model="form.rssLink" autocomplete="off" @change="clearForm"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-form label-width="120px" ref="ruleFormRef2" :rules="rules" v-if="moreFlag">
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" />
      </el-form-item>
      <el-form-item label="博客地址">
        <el-input v-model="form.blogLink" />
      </el-form-item>
      <el-form-item label="文章数量">
        <el-input v-model="form.articleLimit" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="addFollow(fasle)">取消</el-button>
        <el-button type="primary" @click="addFollow(true)"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { readRssOne, addFollow } from "@/common/api/rss";
import { $tip } from "@/base/util/ui";

export default {
  props: {},
  data() {
    return {
      dialogFormVisible: false,
      form: {
        rssLink: "https://r0ad.github.io/atom.xml",
      },
      moreFlag: false,
      rules: {
        rssLink: [
          {
            required: true,
            message: "请输入订阅URL",
            trigger: "change",
          },
        ],
        remark: [
          {
            required: true,
            message: "请输入备注",
            trigger: "change",
          },
        ],
      },
    };
  },
  watch: {},
  methods: {
    addFollowInit() {
      this.moreFlag = false;
      this.dialogFormVisible = true;
    },
    addFollow(flag) {
      if (flag) {
        if (!this.moreFlag) {
          $tip.error("请先点击搜索");
          return false;
        }
        if(!this.form.rssLink || !this.form.remark){
          $tip.error("请输入必填内容");
          return false; 
        }
        
        // 增加订阅
        const param = { ...this.form };
        addFollow(param).then((res) => {
          if (res) {
            $tip.success("添加成功");
            this.dialogFormVisible = false;
            this.$parent.$parent.queryFollow()
          }
        });
      } else {
        this.dialogFormVisible = false;
      }
    },
    onSearch() {
      const rssLink = this.form.rssLink;
      if (!rssLink) {
        $tip.info("请输入订阅URL");
        return false;
      }
      const param = { uri: this.form.rssLink };
      readRssOne(param).then((feed) => {
        this.moreFlag = true;
        this.form.blogLink = feed.link;
        this.form.remark = feed.title;
        this.form.articleLimit = 100;
      });
    },
    clearForm(){
      if(this.moreFlag){
        this.form={rssLink: ""}
        this.moreFlag=false
      }
    }
  },
};
</script>

<style scoped></style>
